package com.odm.communitymanage.ui.mine.MySubmitRepair

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.MyRepairData
import com.odm.communitymanage.core.bean.MyRepairs
import com.odm.communitymanage.core.net.ApiService
import com.rxjava.rxlife.lifeOnMain
import com.zhengsr.tablib.view.adapter.TabFlowAdapter
import com.zhengsr.tablib.view.flow.TabFlowLayout
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_my_idle.*
import kotlinx.android.synthetic.main.fragment_my_submit_repair.*
import kotlinx.android.synthetic.main.fragment_notifications.*
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.RxHttpFormParam
import rxhttp.wrapper.param.RxHttpNoBodyParam

/**
 * @description: 我的报修页面
 * @author: ODM
 * @date: 2020/4/15
 */
class MySubmitRepairFragment : BaseMVVMFragment() {

    private var rvAdapter: MyRepairAdapter ?= null


    private var disposable : Disposable?= null


    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        //默认首先加载未解决列表
        getSubmitRepairData(0)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }

    override fun initViewModelObserve() {

    }

    override fun initViews() {
        initTabLayout()

        ivBack_MySubmitRepair.setOnClickListener {
            pop()
        }

        // 初始化 recyclerView 和 适配器
        rvMyRepairs.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = MyRepairAdapter(mutableListOf())
        rvMyRepairs.adapter = rvAdapter
        rvAdapter?.animationEnable = true

//        //设置空布局
//        rvAdapter?.setEmptyView(R.layout.layout_empty_list)
        rvAdapter?.setOnItemClickListener{ adapter, view, position ->
            LiveEventBus
                .get(Constants.EVENT_MY_REPAIR_DETAIL)
                .post(rvAdapter?.data?.get(position))

            start(MySubmitRepairDetailFragment())

        }
    }


    /**
     * 初始化 TabLayout
     */
    private fun initTabLayout(){
        //TabLayout点击事件
        tabMySubmitRepair?.configTabLayoutConfig {
            //选中项的view的回调
            onSelectViewChange = { fromView, selectViewList, reselect ->
                val toView = selectViewList.first()
                if (reselect) {
                    //重复选择
                }
            }
            //选中项index的回调事件--显示不同排序的瀑布流List
            onSelectIndexChange = { fromIndex, selectIndexList, reselect ->
                //toIndex 为 选中 Index
                val toIndex = selectIndexList.first()
                //显示加载数据状态布局
                msv_my_repair.showLoading()
                getSubmitRepairData(toIndex)
            }
        }
    }


    /**
     * 根据类型获取不同的 报修列表
     * @param type Int
     */
    private fun getSubmitRepairData(type : Int){
        if(type > tabMySubmitRepair.currentItemIndex){
            return
        }
        var param : RxHttpNoBodyParam  = RxHttp.get(ApiService.MY_REPAIR_UNTAKEN)
        when(type){
            0 -> {
              param =   RxHttp.get(ApiService.MY_REPAIR_UNTAKEN)
            }
            1 -> {
                param =   RxHttp.get(ApiService.MY_REPAIR_PROCESSING)

            }
            2 -> {
                param =   RxHttp.get(ApiService.MY_REPAIR_COMPLETED)
            }
            else -> {}
        }
        disposable = param.asClass(MyRepairs::class.java)
            .lifeOnMain(this)
            .subscribe({
                //返回数据有异常情况
                if(it?.code != 1){
                    ToastUtils.showShort(it.message)
                }
                if(it?.data?.isNotEmpty() == true){
                    // 列表数据为空，显示空状态布局
                    // 列表数据不为空显示内容布局，使用setList/setDiffNewData
                    val dataList : MutableList<MyRepairData> = it.data.reversed().toMutableList()
                    msv_my_repair.showContent()
                    if(rvAdapter?.data?.isEmpty() == true) {
                        rvAdapter?.setList(dataList)
                    }else {
                        rvAdapter?.setDiffNewData(DiffUtil.calculateDiff(MyRepairDiffCallback(dataList.toList(),it.data)) ,dataList)
                    }
                }else {
                    msv_my_repair.showEmpty()
                }

            },{
                msv_my_repair.showError()
                ToastUtils.showShort(it?.message)
            })


    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_my_submit_repair
    }


}